import heapq
class Solution(object):
    def findMaxValueOfEquation(self, points, k):
        left = 0
        right = 0
        n = len(points)
        st = []
        max_val = - float('inf')
        while left < n:
            now_val = points[left][1] - points[left][0]
            while right < n and points[right][0] - points[left][0] <= k:
                heapq.heappush(st, [- points[right][0] - points[right][1], right])
                right += 1
            while st and st[0][1] <= left:
                heapq.heappop(st)
            if st:
                max_val = max(max_val, now_val - st[0][0])
            left += 1
        return max_val

data = Solution()
points = [[1,3],[2,0],[5,10],[6,-10]]
k = 1
print(data.findMaxValueOfEquation(points, k))